package com.zhy.baselibrary.view.titlebar

import android.content.Context
import android.graphics.Color

import android.util.AttributeSet
import android.view.View
import android.widget.CheckBox
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zhy.baselibrary.R
import com.zhy.baselibrary.adapter.BaseAdapterHelper
import com.zhy.baselibrary.adapter.recycler.RecyclerAdapter


class TitleBarSelectView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var onSelectTitleListener: OnSelectTitleListener? = null
    private val mData: MutableList<TitleBar> = ArrayList()
    private var rvTitleBar: RecyclerView? = null
    private var selectTextColor = "#C32A2C"
    private var defTextColor = "#535353"
    private var selectLineColor = "#C32A2C"
    private var defLineColor = "#ffffff"
    private var adapter: RecyclerAdapter<TitleBar>? = null

    init {
        initView()
    }


    private fun initView() {
        val view = View.inflate(context, R.layout.title_bar_select_view, null)
        rvTitleBar = view.findViewById<RecyclerView>(R.id.rvTitleBar)
        initAdapter()
        removeAllViews()
        addView(view)
    }


    private fun initAdapter() {
        rvTitleBar?.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true)

        adapter = object :
            RecyclerAdapter<TitleBar>(context, R.layout.item_title_bar_select_view) {
            override fun convert(helper: BaseAdapterHelper, item: TitleBar, position: Int) {
//                val vLine = helper?.getView<View>(R.id.vLine)
                helper?.setText(R.id.tvTitle, "${item?.title ?: ""}")
                val tvTitle = helper?.getView<CheckBox>(R.id.tvTitle)
                if (tvTitle != null) {
                    tvTitle.isSelected = item.isSelect
                }
                if (item.isSelect) {
                    if (tvTitle != null) {
                        tvTitle.setTextColor(Color.parseColor(selectTextColor))
                        tvTitle.textSize = 16f
                        tvTitle.paint.isFakeBoldText = true
                    }
//                    vLine.setBackgroundColor(Color.parseColor(selectLineColor))

                } else {
                    if (tvTitle != null) {
                        tvTitle.paint.isFakeBoldText = false
                        tvTitle.textSize = 14f
                        tvTitle.setTextColor(Color.parseColor(defTextColor))
                    }
//                    vLine.setBackgroundColor(Color.parseColor(defLineColor))
                }
            }
        }
        rvTitleBar?.adapter = adapter
        adapter?.setOnClickItemListener { item, position ->

            if (!item.isSelect) {
                for (titleItem in adapter!!.data) {
                    titleItem.isSelect = false
                }
                item.isSelect = true
                adapter?.notifyDataSetChanged()
                onSelectTitleListener?.onSelectTitle(position, item.titleId, rvTitleBar!!)
            }
        }
    }

    fun initColor(
        selectTextColor: String,
        defTextColor: String,
        selectLineColor: String,
        defLineColor: String
    ) {
        this.selectTextColor = selectTextColor
        this.defTextColor = defTextColor
        this.selectLineColor = selectLineColor
        this.defLineColor = defLineColor
    }

    fun initData(data: MutableList<TitleBar>) {
        adapter?.clear()
        for (item in data) {
            item.isSelect = false
        }
        data[0].isSelect = true
        adapter?.addAll(data)

    }

    fun onSelectTitleListener(onSelectTitleListener: OnSelectTitleListener) {
        this.onSelectTitleListener = onSelectTitleListener
    }


    fun childView(): RecyclerView? {
        return rvTitleBar
    }

    fun changePosition(position: Int) {
        for (index in 0 until adapter!!.data.size) {
            adapter!!.data[index].isSelect = index == position
        }
        adapter?.notifyDataSetChanged()
    }
}